//
//  SideMenuTableView.swift
//  SideMenu
//
//  Created by Jon Kent on 4/5/16.
//  Copyright © 2016 CocoaPods. All rights reserved.
//

import SideMenu

class BESideMenuTableView: UITableViewController {
    
    //定义闭包属性,实现控制器push
    var pushViewControllerBlock: ((_ target: String) -> ())?
    
    lazy var groupArr: [[String: Any]] = {
        let arr = NSArray(contentsOfFile: Bundle.main.path(forResource: "mySettings", ofType: "plist")!)
        return arr! as! [[String : Any]]
    }()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
//        self.navigationController!.navigationBar.setBackgroundImage(UIImage.imageWithColor(UIColor.clearColor(), width: ScreenWidth, height: 44), forBarMetrics: .Default)
        
        self.navigationController?.isNavigationBarHidden = true
        
    }
    
    override func viewDidLoad() {

        setupTableview()
    }
    
    
    private func setupTableview(){
        
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "settingCells")
        
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
        
        tableView.bounces = false
        //把设置tableHeaderView放在此方法中,解决了header遮挡住cell的bug.
        let header = BESideMenuHeader.header()
        tableView.tableHeaderView = header
        tableView.tableFooterView = UIView(frame: CGRect.zero)
        tableView.separatorStyle = .none
        tableView.backgroundColor = UIColor.clear
    }
    
}

//数据源方法
extension BESideMenuTableView{
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return groupArr.count
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if let rowArr = groupArr[section]["Items"] as? NSArray {
            
            return rowArr.count;
        }
        return 0
    }
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "settingCells", for: indexPath as IndexPath)
        
        cell.backgroundColor = UIColor.clear
        cell.selectionStyle = .none
        
        guard let rowArr  = groupArr[indexPath.section]["Items"] as? NSArray else{
            return UITableViewCell()
        }
        
        guard let dataArr = rowArr[indexPath.row] as? [String:AnyObject] else{
            return UITableViewCell()
        }
        
        guard let text = dataArr["Title"] as? String else{
            return UITableViewCell()
        }
        
        guard let imgName = dataArr["AccessoryImgName"] as? String else{
            return UITableViewCell()
        }
        
        cell.textLabel?.font  = baseFont(size: 15)
        cell.textLabel?.textColor = UIColor.white
        cell.textLabel?.text = text
        cell.imageView?.image = UIImage(named: imgName)
        
        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
     
        guard let rowArr  = groupArr[indexPath.section]["Items"] as? NSArray else{
            return
        }
        
        guard let dataArr = rowArr[indexPath.row] as? [String:AnyObject] else{
            return
        }
        
        guard let target = dataArr["Target"] as? String else{
            return
        }
        pushViewControllerBlock!(target)
    }
    
}
